package com.train.course.controller;

import com.train.common.core.controller.BaseController;
import com.train.common.core.domain.AjaxResult;
import com.train.common.utils.SecurityUtils;
import com.train.course.service.ICommentLikeService;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.*;

/**
 * 视频评论点赞 控制器
 */
@RestController
@RequestMapping("/api/v1/comments")
public class VideoCommentLikeController extends BaseController {

    @Autowired
    private ICommentLikeService commentLikeService;

    /**
     * 点赞评论
     */
    @PostMapping("/{commentId}/like")
    public AjaxResult like(@PathVariable("commentId") Long commentId) {
        Long userId = SecurityUtils.getUserId();
        boolean result = commentLikeService.likeComment(userId, commentId);
        return result ? success("点赞成功") : error("点赞失败或已点赞");
    }

    /**
     * 取消点赞
     */
    @DeleteMapping("/{commentId}/like")
    public AjaxResult unlike(@PathVariable("commentId") Long commentId) {
        Long userId = SecurityUtils.getUserId();
        boolean result = commentLikeService.unlikeComment(userId, commentId);
        return result ? success("取消点赞成功") : error("取消点赞失败或未点赞");
    }

    /**
     * 是否已点赞
     */
    @GetMapping("/{commentId}/liked")
    public AjaxResult isLiked(@PathVariable("commentId") Long commentId) {
        Long userId = SecurityUtils.getUserId();
        boolean liked = commentLikeService.isLiked(userId, commentId);
        return success(liked);
    }
}
